<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Goods extends Model
{
    use Searchable;
   protected $guarded = ['product'];
   protected $casts = ['picture'=>'array','category_id'=>'array'];
   public function makeCategory()
   {
     $category_id = $this['category_id'];
     $categoryName = '';
     foreach ($category_id as $value)
     {
        $categoryName .= Category::find($value)->title . '->';
     }
     return rtrim($categoryName,'->');
    }
    public function bale()
    {
        return $this->hasMany(Bale::class,'goods_id','id');
    }

    //关联收藏
    public function collect(){
       return $this->hasOne(Collect::class,'good_id');
    }

    //检测商品是否被当前登录用户收藏
    public function isColledted(){
        return $this->collect()->where('user_id',auth()->id())->first();
    }
}
